package org.liaohailong.library.widget.interact.audio

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.layout_interact_audio_view.view.*
import org.liaohailong.library.R
import org.liaohailong.library.widget.interact.IInteractView
import org.liaohailong.library.widget.interact.Message
import java.lang.Exception
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*

/**
 * Author: liaohailong
 * Date: 2019/3/12
 * Time: 10:20 AM
 * Description: 音频互动插件
 **/
class InteractAudioView(context: Context, attributeSet: AttributeSet?, defStyle: Int) : FrameLayout(context, attributeSet, defStyle),
        AudioPlayer.OnAudioPlayerCallback, IInteractView {
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context) : this(context, null)

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_interact_audio_view, this, true)
    }

    private var timer: Timer? = null
    private var timerTask: TimerTask? = null
    private val date: Date = Date()
    private val timeFormat: DateFormat = SimpleDateFormat("mm:ss", Locale.CHINA)

    private var message: Message? = null
    private var callback: IInteractView.InteractViewCallback? = null

    override fun onAudioPrepared() {
        wave_view.init(AudioPlayer.getAudioSession())
        timer = Timer()
        timerTask = object : TimerTask() {
            override fun run() {
                post {
                    try {
                        val totalDuration = AudioPlayer.getTotalDuration()
                        val currentPosition = AudioPlayer.getCurrentPosition()
                        //设置进度条
                        val progress = currentPosition.toFloat() / totalDuration.toFloat()
                        progress_bar.setProgress(progress)
                        //设置总时长
                        date.time = totalDuration.toLong()
                        val totalTime = timeFormat.format(date)
                        end_time_txt.text = totalTime
                        //设置当前时长
                        date.time = currentPosition.toLong()
                        val currentTime = timeFormat.format(date)
                        start_time_txt.text = currentTime
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            }
        }
        timer?.schedule(timerTask, 0, 1000)
        callback?.onInteractViewPrepared(message!!)
    }

    override fun onAudioComplete() {
        release()
        callback?.onInteractPlayComplete(message!!)
    }

    override fun play(message: Message, callback: IInteractView.InteractViewCallback) {
        this.message = message
        this.callback = callback
        AudioPlayer.addAudioPlayerObserver(this)
        AudioPlayer.play(context.applicationContext, message.url)
    }

    override fun release() {
        wave_view.release()
        AudioPlayer.resetMediaPlayer()
        AudioPlayer.removeAudioPlayerObserver(this)
        timerTask?.cancel()
        timer?.cancel()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        release()
    }
}